import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser'
// @ts-ignore
import Crypt from 'node-jsencrypt';
import { PrivateKey } from '../common';
import mysql from "mysql";
import dayjs from 'dayjs';

const crypt = new Crypt();
crypt.setKey(PrivateKey);

const connection = mysql.createConnection({
  host: "localhost",
  user: "mmorpg",
  password: "mmorpg",
  database: "user"
});
connection.connect();

const app = express();
app.use(cors());
app.use(bodyParser.json());

app.post('/register', function(req, res) {
  console.log("req", req.body);

  let { account, password } = req.body;
  account = crypt.decrypt(account);
  password = crypt.decrypt(password);

  console.log("account", account);
  console.log("password", password);

  connection.query(
    "INSERT INTO user (account,password,created_time) VALUES (?,?,?)",
    [account, password, dayjs().format("YYYY-MM-DD HH:mm:ss")],
    function(error, results, fields) {
      if (error) {
        console.error(error);
        return;
      }
      console.log(results);
    }
  );

  res.json({ });
});

app.get('/', function (req, res) {
  res.send('Hello World');
});

app.listen(3000);

console.log("auth 服务");
